package cn.msqweb.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.msqweb.entity.ProdComm;
import cn.msqweb.model.Result;
import cn.msqweb.service.ProdCommService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 商品评论接口管理
 */
@Api(tags = "商品评论接口管理")
@RequestMapping("prod/prodComm")
@RestController
public class ProdCommController {

    @Resource
    private ProdCommService prodCommService;

    @ApiOperation("多条件分页查询")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:prodComm:page')")
    public Result<Page<ProdComm>> loadProdCommPage(@ApiParam("页码")@RequestParam Long current,
                                                   @ApiParam("条数")@RequestParam Long size,
                                                   @ApiParam("商品名称")@RequestParam(required = false) String prodName,
                                                   @ApiParam("审核状态")@RequestParam(required = false) Integer status){

        Page<ProdComm> page = new Page<>(current,size);
        Page<ProdComm> prodCommPage = prodCommService.page(page, new LambdaQueryWrapper<ProdComm>()
                .like(StringUtils.hasText(prodName), ProdComm::getProdName, prodName)
                .eq(ObjectUtil.isNotNull(status), ProdComm::getStatus, status)
                .orderByDesc(ProdComm::getCreateTime));

        return Result.success(prodCommPage);
    }

    @ApiOperation("根据表示查询商品评论详情")
    @GetMapping("{CommId}")
    @PreAuthorize("hasAuthority('prod:prodComm:info')")
    public Result<ProdComm> loadProdCommInfo(@ApiParam("评论ID")@PathVariable Long CommId){
        ProdComm byId = prodCommService.getById(CommId);
        return Result.success(byId);
    }

    @ApiOperation("回复并审核评论")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:prodComm:update')")
    public Result<String> replyAndReviewProdComm(@ApiParam("评论对象")@RequestBody ProdComm prodComm){
        Boolean replied = prodCommService.replyAndReviewProdComm(prodComm);
        return Result.handler(replied);
    }

}
